package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.blankj.utilcode.util.PermissionUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityAngleCorrectBinding;
import stark.common.basic.utils.StkPermissionHelper;

public class AngleCorrectActivity extends BaseAc<ActivityAngleCorrectBinding> {

    private SensorManager mSensorManager;
    private float mFromDegrees = 0f, mToDegrees;//图片转动时开始、结束角度
    private int mOffset = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_angle_correct;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAngleCorrectBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvAngleCorrectGetPermission.setOnClickListener(this);

        initSensor();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvAngleCorrectGetPermission://获取权限
                getPermission();
                break;
        }
    }

    @Override
    protected void initData() {
        if (PermissionUtils.isGranted(Manifest.permission.CAMERA)) {
            initCamera();
            mDataBinding.tvAngleCorrectGetPermission.setVisibility(View.GONE);
        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(Manifest.permission.CAMERA)
                .reqPermissionDesc(getString(R.string.get_camera_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        mDataBinding.tvAngleCorrectGetPermission.setVisibility(View.GONE);
                        initCamera();
                    }
                }).request();
    }

    private void initCamera() {
        mDataBinding.cameraView.setLifecycleOwner(this);
        mDataBinding.cameraView.setRequestPermissions(false);
    }

    private void initSensor() {
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mSensorManager = sensorManager;
        //通过 getDefaultSensor 获得指南针传感器
        Sensor sensor = sensorManager.getDefaultSensor(SensorManager.SENSOR_DELAY_GAME);
        //为传感器管理者注册监听器，第三个参数指获取速度正常
        sensorManager.registerListener(mSensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    private void deInitSensor() {
        if (mSensorManager != null) {
            mSensorManager.unregisterListener(mSensorEventListener);
        }
    }

    private void setDegreesText(int degrees) {
        if (degrees > (mOffset - 2) && degrees < (mOffset + 2)) {
            mDataBinding.tvTip.setText(R.string.pendant_correct_tip);
            mDataBinding.tvTip.setTextColor(Color.BLUE);
        } else {
            mDataBinding.tvTip.setText(R.string.pendant_no_correct_tip);
            mDataBinding.tvTip.setTextColor(Color.RED);
        }

    }

    private SensorEventListener mSensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            switch (sensorEvent.sensor.getType()) {
                case SensorManager.SENSOR_DELAY_GAME:
                    //顺时针转动为正，故手机顺时针转动时，图片得逆时针转动
                    mToDegrees = -sensorEvent.values[0] * 10;
                    setDegreesText((int) -mToDegrees);
                    //让图片相对自身中心点转动，开始角度默认为0；此后开始角度等于上一次结束角度
                    RotateAnimation ra = new RotateAnimation(mFromDegrees + mOffset, mToDegrees + mOffset, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    //动画时间200毫秒
                    ra.setDuration(200);
                    ra.setFillAfter(true);
                    mDataBinding.ivUp.startAnimation(ra);
                    mDataBinding.ivDown.startAnimation(ra);

                    mFromDegrees = mToDegrees;
                    break;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        deInitSensor();
    }

}